﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Web;
using Jumbot.Common;
using Jumbot.DataOper;
namespace JumbotCms.DataObject
{
    public class Tag
    {
        private string m_Id = "0";
        private int m_ChannelId = 0;
        private string m_Title = string.Empty;
        private int m_ClickTimes = 0;

        public string Id
        {
            get { return m_Id; }
            set { m_Id = value; }
        }
        public int ChannelId
        {
            set { m_ChannelId = value; }
            get { return m_ChannelId; }
        }
        public string Title
        {
            set { m_Title = value; }
            get { return m_Title; }
        }
        public int ClickTimes
        {
            set { m_ClickTimes = value; }
            get { return m_ClickTimes; }
        }

        public Tag()
        { }
        public void GetAllColumn(string _id, DbOperHandler doh)
        {
            doh.Reset();
            doh.SqlCmd = "SELECT * FROM [jcms_normal_tag] WHERE [Id]=" + _id;
            DataTable dt = doh.GetDataTable();
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                Id = _id;
                ChannelId = Validator.StrToInt(dr["ChannelId"].ToString(), 0);
                Title = dr["Title"].ToString();
                ClickTimes = Validator.StrToInt(dr["ClickTimes"].ToString(), 0);
            }
        }

        public int GetChannelId(string _id, DbOperHandler doh)
        {
            doh.Reset();
            doh.SqlCmd = "SELECT [ChannelId] FROM [jcms_normal_tag] WHERE [Id]=" + _id;
            DataTable dt = doh.GetDataTable();
            if (dt.Rows.Count > 0)
            {
                return Validator.StrToInt(dt.Rows[0]["ChannelId"].ToString(), 0);
            }
            return 0;
        }
        public string GetTitle(string _id, DbOperHandler doh)
        {
            doh.Reset();
            doh.SqlCmd = "SELECT [Title] FROM [jcms_normal_tag] WHERE [Id]=" + _id;
            DataTable dt = doh.GetDataTable();
            if (dt.Rows.Count > 0)
            {
                return dt.Rows[0]["Title"].ToString();
            }
            return string.Empty;
        }
        public int GetClickTimes(string _id, DbOperHandler doh)
        {
            doh.Reset();
            doh.SqlCmd = "SELECT [ClickTimes] FROM [jcms_normal_tag] WHERE [Id]=" + _id;
            DataTable dt = doh.GetDataTable();
            if (dt.Rows.Count > 0)
            {
                return Validator.StrToInt(dt.Rows[0]["ClickTimes"].ToString(), 0);
            }
            return 0;
        }
        public void InsertTag(string _channelID, string _tags, DbOperHandler doh)
        {
            if (_tags.Length == 0) return;
            doh.Reset();
            string[] tag = _tags.Split(',');
            for (int i = 0; i < tag.Length; i++)
            {
                //tag[i] = Jumbot.Common.Strings.SafetyStr(tag[i].ToString());
                doh.Reset();
                doh.ConditionExpress = "title=@title and ChannelId=" + _channelID;
                doh.AddConditionParameter("@title", tag[i].ToString());
                if (!doh.Exist("jcms_normal_tag"))
                {
                    doh.Reset();
                    doh.AddFieldItem("Title", tag[i].ToString());
                    doh.AddFieldItem("ClickTimes", "0");
                    doh.AddFieldItem("ChannelId", _channelID);
                    doh.Insert("jcms_normal_tag");
                }
            }
        }
    }
}
